home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 8 / tidy.zip / NJTIDY.C next >
C/C++ Source or Header  |  1987-12-07  |  3KB  |  119 lines

  1. /***************************************************************************
  2. *                                       *
  3. *    Nifty James' Famous Disk Tidy Program                   *
  4. *                                       *
  5. *    (C) Copyright 1987 by Mike Blaszczak All Rights Reserved       *
  6. *                                       *
  7. *    Version 1.00 of 7 December 1987                       *
  8. *    Written for the Microsoft Optimizing C Compiler, Version 5.00       *
  9. *            (Use the COMPACT model)                   *
  10. *                                       *
  11. ***************************************************************************/
  12.  
  13. #include <dos.h>
  14. #include <malloc.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. #include <noenv.h>
  19. #include <noargs.h>
  20.  
  21. #define    MAXSTACK    7168
  22. #define    MAX_PATH_LEN    64
  23.  
  24. char    *subdirlist[MAXSTACK];        /* list of subdirectories     */
  25. int     topdir = 1;            /* first free subdirlist entry    */
  26. int    worktop = 0;            /* used as iteration counter    */
  27.  
  28. int    index;                /* loop variable        */
  29.  
  30. char    tempname[MAX_PATH_LEN];        /* temporary name for file dels */
  31.  
  32. long    deleted =0L;            /* number of files deleted    */
  33. unsigned long totalbytes =0L;        /* number of bytes freed    */
  34.  
  35. unsigned        result;            /* result of _dos operations    */
  36. struct  find_t  c_file;            /* structure for _dos ops    */
  37.  
  38. /* ---------------------------------------------------------------------- */ 
  39.  
  40. int deleteone(n)
  41. int n;
  42. {
  43.     strcpy(tempname, subdirlist[n]);
  44.     tempname[strlen(tempname)-3] = '\0';
  45.     strcat(tempname, c_file.name);
  46.  
  47.     printf("Deleting file %s", tempname);
  48.     return(remove(tempname));
  49. }    
  50.  
  51. /* ---------------------------------------------------------------------- */ 
  52.  
  53. void main()
  54. {
  55.     puts("Nifty James Famous Disk Tidyer");
  56.     puts("Version 1.00 of 07 December 1987");
  57.     puts("(C)Copyright 1987 by Mike Blaszczak\n");
  58.  
  59.     subdirlist[0] = "\\*.*";
  60.  
  61.     while(worktop<topdir)    {    
  62.     result = _dos_findfirst(subdirlist[worktop], _A_SUBDIR, &c_file);
  63.  
  64.         while(result == 0)
  65.         {
  66.             if (strcmp(c_file.name, ".") != 0  &&
  67.                 strcmp(c_file.name, "..") != 0 &&
  68.                 c_file.attrib == _A_SUBDIR)
  69.             {
  70.                 if ( topdir == MAXSTACK ||
  71.                     (subdirlist[topdir] = (char *) malloc(MAX_PATH_LEN)) == NULL )
  72.                 {
  73.                     puts("NJTIDY: Out of memory error\n");
  74.                     exit(1);
  75.                 }
  76.                 strcpy(subdirlist[topdir], subdirlist[worktop]);
  77.                 subdirlist[topdir][strlen(subdirlist[topdir])-3] = '\0';
  78.                 strcat(subdirlist[topdir], c_file.name);
  79.                 strcat(subdirlist[topdir++], "\\*.*");
  80.             }
  81.             result = _dos_findnext(&c_file);
  82.         }
  83.         worktop++;
  84.     }
  85.  
  86.     for(index = 0; index < topdir; index++)
  87.     {
  88.         result = _dos_findfirst(subdirlist[index], _A_NORMAL, &c_file);
  89.  
  90.         while(result == 0)
  91.         {
  92.             if (strstr(c_file.name, ".BAK") != NULL)
  93.             {
  94.                 deleteone(index);
  95.                 putchar('\n');
  96.                 deleted++;
  97.                 totalbytes += c_file.size;
  98.             }
  99.             else
  100.                 if (c_file.size == 0L && c_file.attrib != _A_SUBDIR)
  101.                 {
  102.                     deleteone(index);
  103.                     puts(" (file's length was zero)");
  104.                     deleted++;
  105.                 }
  106.             result = _dos_findnext(&c_file);
  107.         }
  108.     }        
  109.  
  110.     if(deleted == 0L)
  111.         puts("\nNo files were deleted\n");
  112.     else
  113.         printf("\n%ld files were deleted for a total of %lu bytes freed.\n",
  114.             deleted, totalbytes);
  115.  
  116.     exit(0);
  117. }
  118.  
  119.